package com.ruoyi.web.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class TestSignImg {

    /**
     * 根据str,font的样式等生成图片
     * https://blog.csdn.net/sinat_28505133/article/details/54669111
     *
     * @param strArr
     * @param font
     * @throws Exception
     */
    public static ImgBean createTextImage(String[] strArr, Font font, int every_line,int lineNum,Color fontColor) throws Exception {
        FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(font);
        int stringWidth = fm.charWidth('字');// 标点符号也算一个字
        int image_width  = lineNum * stringWidth;
        int image_height = stringWidth * every_line + 10;

        int line_height = stringWidth ;

        int line_string_num =lineNum;

        ArrayList<String> listStr = new ArrayList<>();
        ArrayList<String> newList = new ArrayList<>();

        for (int h = 0; h < strArr.length; h++) {
            listStr.add(strArr[h]);
        }
        for (int j = 0; j < listStr.size(); j++) {
            if( listStr.get(j).length() > line_string_num){
                newList.add(listStr.get(j).substring(0,line_string_num));
                listStr.add(j+1,listStr.get(j).substring(line_string_num));
                listStr.set(j,listStr.get(j).substring(0,line_string_num));
            }else{
                newList.add(listStr.get(j));
            }
        }
        int imgNum = 1;
        ByteArrayOutputStream outStream =new ByteArrayOutputStream();
        for (int m = 0; m < imgNum; m++) {

            // 创建图片
            BufferedImage image = new BufferedImage(image_width, image_height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            //设置背景透明
            image = g.getDeviceConfiguration().createCompatibleImage(image_width, image_height, Transparency.TRANSLUCENT);
            g.dispose();
            g = image.createGraphics();
            //开启文字抗锯齿
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            //设置字体
            g.setFont(font);
            //设置字体颜色
            g.setColor(fontColor);
            //设置透明度:1.0f为透明度 ，值从0-1.0，依次变得不透明
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,  1.0f));
//            g.fillRect(0, 0, width, image_height);
            // 每张多少行，当到最后一张时判断是否填充满
            for (int i = 0; i < every_line; i++) {
                int index = i + m * every_line;
                if (newList.size() - 1 >= index) {
                    g.drawString(newList.get(index), 0, line_height * (i + 1));
                }
            }
            g.dispose();
//            ImageIO.write(image, "jpg", outFile);// 输出png图片
            ImageIO.write(image, "gif", outStream);
        }
        return new ImgBean(outStream.toByteArray(),image_width,image_height);
    }


    public static ImgBean  createTextImage(String msg,String font,int fontSize,int lineNum,Color fontColor)  {

        String[] strArr = msg.split("\n");
        if (msg.length()<lineNum){
            lineNum = msg.length();
        }
        float stringLength = msg.length();
        float l = lineNum;
        int rowNum =(int)Math.ceil(stringLength/l);
        ImgBean img = null;
        try {
            img = createTextImage(strArr, new Font(font, Font.PLAIN, fontSize),rowNum,lineNum,fontColor);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return img;
    }

    /**
     * Color对象转换成字符串
     * @param color Color对象
     * @return 16进制颜色字符串
     * */
    private static String toHexFromColor(Color color){
        String r,g,b;
        StringBuilder su = new StringBuilder();
        r = Integer.toHexString(color.getRed());
        g = Integer.toHexString(color.getGreen());
        b = Integer.toHexString(color.getBlue());
        r = r.length() == 1 ? "0" + r : r;
        g = g.length() ==1 ? "0" +g : g;
        b = b.length() == 1 ? "0" + b : b;
        r = r.toUpperCase();
        g = g.toUpperCase();
        b = b.toUpperCase();
        su.append("#");
        su.append(r);
        su.append(g);
        su.append(b);
        //0xFF0000FF
        return su.toString();
    }
    /**
     * 字符串转换成Color对象
     * @param colorStr 16进制颜色字符串
     * @return Color对象
     * */
    public static Color toColorFromString(String colorStr){
        colorStr = colorStr.substring(1);
        Color color =  new Color(Integer.parseInt(colorStr, 16)) ;
        //java.awt.Color[r=0,g=0,b=255]
        return color;
    }

    public static void main(String[] args) throws FileNotFoundException {
//        //审批意见 自定义图片
//        String msg = "我不是我不是我不是是我不是我是我不是我是我不是我是我不是我是我不是我是我不是我是我不是我是我不是我是我不是我是我不是我";
//        int lineNum = 6;
//        String font = "楷体";
//        ImgBean img = createTextImage(msg,font,100,lineNum);
////        byte[] imgByte = createTextImage(msg,font,lineNum);
//        byte2File(img.getImgData(),"D:\\demo1.gif");

        System.out.println(toHexFromColor(Color.red));

        System.out.println(toColorFromString("#0000FF"));

    }
}